$(function(){
	setTimeout( __cardActivexInit, 13 );
});

function checkActivex() {
	var o = document.getElementById("bdLockCard");
	return o.Hotel != null;
}

function __cardActivexInit() {
	var o = document.getElementById("bdLockCard");
	if (o == null || o.inited === true)
		return;
	var c = window.Context.Card;
	o.Hotel =  c.Name;
	o.HotelPwd = c.Key;
	o.FPortNo = c.Port;
	o.FReaderType = c.Reader;
	o.FCardType =  c.Type;
	o.FSector = c.Sector;
	o.FBlock = c.Block;
	o.inited = true;
}

function __Card__() {
	/** 获取ActiveX插件,内部使用 */
	function getActiveX() {
		var o = document.getElementById("bdLockCard");
		if (o == null) {
			if (window.Context.Test) {
				return {
					readSnr:function(){},
					readCard:function(){},
					writeCard:function(){},
					SNR:'123456',
					LockNo:'LockNo1',
					FTime:'1008311020',
					CardID:'13888',
					SubCardID:'502'
				};
			}
			alert("插件未就续", null);
			return null;
		} else {
			if (o.inited !== true)
				__cardActivexInit();
		}
		var a = typeof o.readCard;
		if (a === 'undefined') {
//			window.alert("请确认您的读卡器插件已经安装.", null);
			return null;
//		} else {
//			alert(a);
		}
//		if (a !== 'unknown') {
//			alert(a);
//		}
		return o;
	};

	/** 读取卡序列号 */
	this.readSerial=function () {
//		return "2F177F3C";
//		return "2D1C7F3C";
		var o = getActiveX();
		if (o == null)
			return null;
		o.readSnr();
		var snr = o.SNR;
		if (snr === null || snr === '')
			return null;
		return snr;
	};

	/** 确认当前的卡序列号 */
	this.check=function (serial) {
		return this.readSerial() === serial;
	};

	/**
	 * 读卡, 含序列号
	 * */
	this.readDetail=function () {
		var serial = this.readSerial();
		if (serial == null) {
			return null;
		}
		var o = getActiveX();
		if (o == null)
			return [serial, '1', '2', '3', '4'];
		o.readCard();
		var l = "" + o.LockNo;
		if (l == '65535')
			l = '';
		var t = '' + o.FTime;
		if (t == '0000000000')
			t = '';
		var r = [serial, l, t];		//, o.CardID, o.SubCardID, o.StartSuite, o.EndSuite
		return r;
	};

	/** 读卡, 不含序列号 */
	this.read=function () {
		var o = getActiveX();
		if (o == null)
			return;
		o.readCard();
		var l = "" + o.LockNo;
		if (l == '65535')
			l = '';
		var t = '' + o.FTime;
		if (t == '0000000000')
			t = '';
		var r = [l, t];			//, o.CardID, o.SubCardID, o.StartSuite, o.EndSuite
		return r;
	};

//	/** 读取卡号 */
//	this.readCardNumber=function() {
//		var r = this.read();
//		return r == null ? null : r[2];
//	};
//
//	/** 读取卡的锁号 */
//	this.readLockNumber=function() {
//		var r =this.read();
//		return r == null ? null : r[0];
//	};
	
	/**
	 * 写卡
	 * (读一次, 写一次)
	 * */
	this.write=function(serial, lock, time) {//, cardNumber, subCardId, startSuite, endSuite
		if (lock === undefined)
			return false;
		if (!this.check(serial))
			return false;
		var o = getActiveX();
//		if (nonRead !== true)
//			this.read();
		if (lock !== null && lock !== undefined)
			o.LockNo = lock;
		if (time !== null && time !== undefined)
			o.FTime = time;
//		if (cardNumber !== null && cardNumber !== undefined)
//			o.CardID = cardNumber;
//		if (subCardId !== null && subCardId !== undefined)
//			o.SubCardID = subCardId;
//		if (startSuite !== null && startSuite !== undefined)
//			o.StartSuite = startSuite;
//		if (endSuite !== null && endSuite !== undefined)
//			o.EndSuite = endSuite;
		o.writeCard();
		return true;
	};
	
	/**
	 * 直接写卡, 不校验
	 * @return 卡序列号
	 */
	this.writeDirectly=function(lock, time) {//, cardNumber, subCardId
		if (lock === undefined)
			return false;
		var o = getActiveX();
		o.LockNo = lock;
		o.FTime = time;
//		if (lock !== null && lock !== undefined)
//		if (time !== null && time !== undefined)
//		if (cardNumber !== null && cardNumber !== undefined)
//			o.CardID = cardNumber;
//		if (subCardId !== null && subCardId !== undefined)
//			o.SubCardID = subCardId;
		o.writeCard();
		return true;
	};
	this.writeCardMessage=function(serial, lock, time, cardNumber, subCardId ,message) {
		if (lock === undefined)
		{
			alert(message+"失败!锁号未定义");
			return false;
		}
		if (!this.check(serial)){
			alert(message+"失败!");
			return false;
		}
		var o = getActiveX();
		//this.read();		//XXX 根据需要和条件, 可能不需要调用些方法
		if (lock !== null && lock !== undefined)
			o.LockNo = lock;
		if (time !== null && time !== undefined)
			o.FTime = time;
//		if (cardNumber !== null && cardNumber !== undefined)
//			o.CardID = cardNumber;
//		if (subCardId !== null && subCardId !== undefined)
//			o.SubCardID = subCardId;
//		if (startSuite !== null && startSuite !== undefined)
//			o.StartSuite = startSuite;
//		if (endSuite !== null && endSuite !== undefined)
//			o.EndSuite = endSuite;
		o.writeCard();
		alert(message);
	};

}


var Card = window.Card = new __Card__();
